package com.ulic.customerManApplication.studentmodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.membermodule.bo.SubMember;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.studentmodule.bo.SubStudent;
import com.ulic.customerManApplication.studentmodule.dao.SubStudentDao;
import com.ulic.customerManApplication.studentmodule.vo.SubStudentVO;
import com.ulic.customerManApplication.usermodule.bo.User;
@Component("subStudentDao")
public class SubStudentDaoImpl extends BaseDao implements SubStudentDao {

	Session session = null;

	@Override
	public void deleteSubStudent(SubStudent subStudent) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(subStudent);
		session.flush();
	}

	@Override
	public SubStudent getSubStudent(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(SubStudent.class);
		List list = cri.add(Restrictions.eq("sub_id", id)).list();
		SubStudent subStudent = null;
		if(list.size()>0){
			subStudent	= (SubStudent)list.get(0);
		}
		return subStudent;
	}

	@Override
	public PagerModel getSubStudents(int curPage) {
		return null;
	}

	@Override
	public List<SubStudent> getSubStudents(User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(SubStudent.class);
		List list = cri.createAlias("sub_user", "u").add(Restrictions.eq("u.um_user_id", user.getUm_user_id())).list();
		return list;
	}

	@Override
	public PagerModel getSubStudents(SubStudentVO subStudentVO, int curPage) {

		StringBuilder sb = new StringBuilder();
		sb.append(" from SubStudent subStudent where subStudent.sub_journeyOfLoveActivity.id="+subStudentVO.getJourneyLoveId());
		SubStudent subStudent = subStudentVO.getSubStudent();
		if(subStudent != null){
			if(subStudent.getSub_name()!= null && subStudent.getSub_name().length() !=0){
				sb.append(" and subStudent.sub_name  like '%"+subStudent.getSub_name().trim()+"%'");
			}
			if(! "No".equals(subStudent.getSub_gender())){
				sb.append(" and subStudent.sub_gender ='"+subStudent.getSub_gender()+"'");
			}
			if(subStudent.getSubCompany()!= null){
				if(! "No".equals(subStudent.getSubCompany()))
				sb.append(" and subStudent.subCompany= '"+subStudent.getSubCompany()+"'");
			}
			if(subStudent.getSubCity()!= null){
				if(! "No".equals(subStudent.getSubCity()))
				sb.append(" and subStudent.subCity= '"+subStudent.getSubCity()+"'");
			}
			if(subStudentVO.getStartcreate()!=null &&subStudentVO.getStartcreate().length()!=0){
				sb.append(" and subStudent.sub_createDate>= to_date('"+subStudentVO.getStartcreate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(subStudentVO.getEndcreate()!=null &&subStudentVO.getEndcreate().length()!=0){
				sb.append(" and subStudent.sub_createDate <= to_date('"+subStudentVO.getEndcreate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by sub_createDate desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public void insertSubStudent(SubStudent subStudent) throws Exception {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.save(subStudent);
		session.flush();
	}

	@Override
	public void insertSubStudents(List<SubStudent> subStudents)
			throws Exception {

		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		for(SubStudent subStudent:subStudents){
			session.save(subStudent);
		}
		session.flush();
	}

	@Override
	public void updateSubStudent(SubStudent subStudent) throws Exception {

		try{
        	session = getHibernateTemplate().getSessionFactory().getCurrentSession();
        	session.merge(subStudent);
        	session.flush();
        }catch (Exception e) {
			// TODO: handle exception
        	e.printStackTrace();
		}
	}

	@Override
	public List<SubStudent> getSubStudentsByJourId(Integer journeyLoveId) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(SubStudent.class);
		List list = cri.createAlias("sub_journeyOfLoveActivity", "jour").add(Restrictions.eq("jour.id", journeyLoveId)).list();
		return list;
	}

}
